package com.ccx.coachsite.ui.share;


import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.api.CoachSiteSoftMgrApiAccessor;
import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.downloadmgr.DownloadTool;
import com.ccx.coachsite.model.CSResultCodeMap;
import com.ccx.coachsite.model.ServerDetailResult;
import com.ccx.coachsite.model.SoftShareBySmItem;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.util.OpenShareTool;
import com.ccx.coachsite.util.SharedPreferencesUtil;

public class OpenShareDetail extends Activity implements OpenSendListener{
	
	private static final int SHARE_SUCC = 201;
	
	private static final int SHARE_FAIL = 202;
	
	private String mUserId = null;
	
	//一键分享时
	private ProgressDialog shWeiboPd;
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			ServerDetailResult<SoftShareBySmItem> toastObj = null;
			
			switch (msg.what) {
				
			case SHARE_SUCC:
				if (shWeiboPd!=null){
					shWeiboPd.dismiss();
				}
				
				toastObj = (ServerDetailResult<SoftShareBySmItem>)msg.obj;
				int succSize = toastObj.detailItem.successList.size();
				int errorSize = toastObj.detailItem.errorList.size();
				if ( errorSize==0 && succSize>0 ) {
					Toast.makeText(OpenShareDetail.this, OpenShareTool.getShareDesc(OpenShareDetail.this)+"成功", Toast.LENGTH_LONG).show();
				}else if (succSize>0 && errorSize>0){
					Toast.makeText(OpenShareDetail.this, OpenShareTool.getShareDesc(OpenShareDetail.this)+"成功"+succSize+"条，失败"+errorSize+"条", Toast.LENGTH_LONG).show();	
				}else if (succSize==0){
					//如果发送单个用户失败
					if (errorSize==1) {
						Toast.makeText(OpenShareDetail.this, "该客户已拒绝接收"+OpenShareTool.getShareDesc(OpenShareDetail.this)+"短信,"+OpenShareTool.getShareDesc(OpenShareDetail.this)+"失败", Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(OpenShareDetail.this, OpenShareTool.getShareDesc(OpenShareDetail.this)+"失败", Toast.LENGTH_LONG).show();
					}
				}
				finish();
				break;
			
			case SHARE_FAIL:
				if (shWeiboPd!=null){
					shWeiboPd.dismiss();
				}
				
				toastObj = (ServerDetailResult<SoftShareBySmItem>)msg.obj;		
				if (toastObj!=null) {
					String errorInfo = CSResultCodeMap.getCodeDesc(toastObj.getResultCode());
					Toast.makeText(OpenShareDetail.this, errorInfo, Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(OpenShareDetail.this, OpenShareTool.getShareDesc(OpenShareDetail.this)+"失败", Toast.LENGTH_LONG).show();
				}
				finish();
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.open_share_main);
		
		//1、消息推送  2、短信分享
		int currentType = getIntent().getIntExtra("currentType",1);
		
		FrameLayout contentFrame = (FrameLayout)findViewById(R.id.contentFrame);
		if (contentFrame!=null){
			contentFrame.addView(new OpenShareTabLazyView(this, this, currentType));
		}
	}

	@Override
	public void sendSMS(final int tabID, String teleIds) {
		
		if (!LoginHelper.requireLogin(this))
			return;
		
		shWeiboPd = new ProgressDialog(this);
		shWeiboPd.setMessage("正在"+OpenShareTool.getShareDesc(this)+"...");
		shWeiboPd.show();
		
		mUserId = SharedPreferencesUtil.getValue(this, SharedKey.Shared_UserId_Key, mUserId);
		
		boolean isExport = SharedPreferencesUtil.getBoolean(this, SharedKey.Shared_Is_Expert);
		
		final String softId = getIntent().getStringExtra("infoID");
		final String userType = isExport?"0":"1";
		final int shareType = getIntent().getIntExtra("shareType",1);//1.教程 2应用 3游戏 4专题
		final String dialogTitle = getIntent().getStringExtra("infoName");
		final String category = getIntent().getStringExtra("category");
		final int currentType = getIntent().getIntExtra("currentType",1);// 2、短信分享
		final String softFlag = DownloadTool.getSoftFlag(getIntent().getStringExtra(DownloadTool.SOFT_FLAG));
		
		if (teleIds!=null){
			teleIds = teleIds.replaceAll(" ", "");
			teleIds = teleIds.replaceAll("-", "");
		}
		
		final String toShareUserIds = teleIds;
		
		//短信分享接口
		if (currentType==2){
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					
					//1为分组用户2为手机号码或通讯录
					String smsFlag = "2";
					if ( tabID==2 ) {//当选中的为Tab时是分组用户
						smsFlag = "1";
					}
					
					ServerDetailResult<SoftShareBySmItem>  serverDetailResult = 
							CoachSiteSoftMgrApiAccessor.shareInfoToUsersBySMS(mUserId, toShareUserIds, softId, userType, shareType, "", smsFlag, softFlag);
					
					int handleCode = SHARE_SUCC;
					if (serverDetailResult.isbNetworkProblem() || !serverDetailResult.detailItem.isSuccessful){
						handleCode = SHARE_FAIL;
					}
					
					Message msg=new Message();
					msg.what=handleCode;
					msg.obj=serverDetailResult;
					handler.sendMessage(msg);	
				}
			}).start();
			
			return ;
		}
		
		//消息推送
		if(currentType==1){
			Account acount = AccountMgr.readAccountInfo(this);
			String appShareText = "";
			
			//1.教程 2应用 3游戏 4专题
			switch (shareType) {
			case SoftShareMessage.TYPE_TUTORIAL_SHARE_TYPE:
				appShareText = getString(R.string.soft_mgr_share_appower_tutorial);
				appShareText=appShareText.replace("{0}", dialogTitle);
				break;
			case SoftShareMessage.TYPE_APP_SHARE_TYPE:
				appShareText = getString(R.string.soft_mgr_share_appower_soft);
				appShareText=appShareText.replace("{0}", dialogTitle);
				break;
			case SoftShareMessage.TYPE_GAME_SHARE_TYPE:
				appShareText = getString(R.string.soft_mgr_share_appower_game);
				appShareText=appShareText.replace("{0}", dialogTitle);
				break;
			case SoftShareMessage.TYPE_TOPIC_SHARE_TYPE:
				appShareText = getString(R.string.soft_mgr_share_appower_topic);
				appShareText=appShareText.replace("{0}", dialogTitle);
				break;
			default:
				break;
			}
			final String shareContent = appShareText;
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					
					ServerDetailResult<Boolean>  serverDetailResult = 
							CoachSiteSoftMgrApiAccessor.shareInfoToUsersByPost(mUserId, toShareUserIds, softId, userType, shareType, shareContent, softFlag);
					
					int handleCode = SHARE_SUCC;
					if (serverDetailResult.isbNetworkProblem() || !serverDetailResult.detailItem){
						handleCode = SHARE_FAIL;
						showToast(OpenShareTool.getShareDesc(OpenShareDetail.this)+"失败");
					}else{
						showToast(OpenShareTool.getShareDesc(OpenShareDetail.this)+"成功");						
					}
				}
			}).start();
		}
		
		switch (tabID) {
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
		}
	}
	
	public static String getShareUserIds(List<String> teleList){
		
		String resultUserIds = "";

		if (teleList==null){
			return resultUserIds;
		}
			
		if ( teleList.size()>0 ){
			
			for(int i=0; i<teleList.size(); i++){
				String s = (String) teleList.get(i);
				resultUserIds = resultUserIds+s+",";
			}
	        
	        if ( resultUserIds.endsWith(",") ){
	        	resultUserIds = resultUserIds.substring(0, resultUserIds.length()-1);
	        }
		}
		
		return resultUserIds;
	}
	
	private void showToast(final String info){
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				
				if (shWeiboPd!=null){
					shWeiboPd.dismiss();
				}
				
				Toast.makeText(OpenShareDetail.this, info+"", Toast.LENGTH_LONG).show();	
				
				finish();
			}
		});
	}
}
